package com.loadmore.common.widget;

import android.content.Context;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.viewpager.widget.PagerAdapter;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;

import java.util.ArrayList;
import java.util.List;

public class CardPager extends FixedViewPager {
    private CardPagerAdapter adapter;
    public List list;
    private List<View> views = new ArrayList<>();
    public CardPager(@NonNull Context context) {
        super(context,null);

    }

    public CardPager(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);

    }

    public void setData(List list) {
        this.views.clear();
        int count = list.size();
        this.list = list;
        List data = new ArrayList();
        int page = 0;
        for (int i = 0; i < count; i++) {
            data.add(list.get(i));
            if (data.size() >=8 || count-1 == i) {
                views.add(getRecyclerView(data,page));
                data.clear();
                page++;
            }
        }
        adapter = new CardPagerAdapter();
        setAdapter(adapter);
        setOffscreenPageLimit(views.size());

    }


    public void notifyDataSetChanged() {
        this.views.clear();
        int count = list.size();
        List data = new ArrayList();
        int page = 0;
        for (int i = 0; i < count; i++) {
            data.add(list.get(i));
            if (data.size() >=8 || count-1 == i) {
                RecyclerView recyclerView = getRecyclerView(data,page);
                views.add(recyclerView);
                data.clear();
                page++;
            }
        }
        adapter.notifyDataSetChanged();
        setOffscreenPageLimit(views.size());
    }

    public Object getItem(int index) {
        return list.get(index);
    }

    public RecyclerView getRecyclerView(List data, final int page) {
        final CardAdapter adapter = new CardAdapter(getContext());
        RecyclerView recyclerView = new RecyclerView(getContext());
        RecyclerView.LayoutManager layoutManager = new GridLayoutManager(getContext(),4);
        recyclerView.setNestedScrollingEnabled(false);
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setFocusable(false);
        adapter.append(data);
        adapter.setClickListener(new CardAdapter.ClickListener() {
            @Override
            public void onClick(int position) {
                int index = page*8+position;
             //   list.remove(index);
//                adapter.removeItem(position);
//                notifyDataSetChanged();
                if (cardClickListener != null) {
                    cardClickListener.onClick(getItem(index));
                }

            }
        });
        recyclerView.setAdapter(adapter);
        return recyclerView;
    }

    private CardClickListener cardClickListener;

    public void setCardClickListener(CardClickListener cardClickListener) {
        this.cardClickListener = cardClickListener;
    }

    public interface CardClickListener{
        void onClick(Object object);
    }

    class CardPagerAdapter extends PagerAdapter {

        @Override
        public int getCount() {
            return views.size();
        }

        @Override
        public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
            return view == object;
        }

        @NonNull
        @Override
        public Object instantiateItem(@NonNull ViewGroup container, int position) {
            View view = views.get(position);
            container.addView(view);
            return view;
        }

        @Override
        public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
            container.removeView((View) object);
        }

        @Override
        public int getItemPosition(Object object) {
            return POSITION_NONE;
        }
    }
}
